package com.syq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syq.mapper.DtsStorageMapper;
import com.syq.pojo.DtsStorage;
import com.syq.service.DtsStorageService;
import com.syq.util.AliyunOSSUtil;
import com.syq.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: DtsStorageServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/29 9:36
 */
@Transactional
@Service
public class DtsStorageServiceImpl implements DtsStorageService {
    @Value("http://oss-cn-beijing.aliyuncs.com")
    private String aliyunOssEndpoint;
    @Value("LTAI5t8DDfbnUGZF2mtc2LUH")
    private String aliyunOssAccessKeyId;
    @Value("ekJZG9oRWyn24VzkQbynPbSt57SBZS")
    private String aliyunOssAccessKeySecret;
    @Value("syq74")
    private String aliyunOssBucketName;

    @Autowired
    private DtsStorageMapper dtsStorageMapper;


    @Override
    public DtsStorage uploadFile(byte[] file, String ext, int size, String originalFilename) throws Exception {
        String fileName = UUID.randomUUID().toString() + "." + ext;
        String url = AliyunOSSUtil.uploadImage(aliyunOssEndpoint,
                aliyunOssAccessKeyId,
                aliyunOssAccessKeySecret,
                aliyunOssBucketName,
                fileName,
                file);
        DtsStorage dtsStorage=new DtsStorage();
        dtsStorage.setUrl(url);
        dtsStorage.setAddTime(new Date());
        dtsStorage.setUpdateTime(new Date());
        dtsStorage.setKey(fileName);
        dtsStorage.setType(ext);
        dtsStorage.setName(originalFilename);
        dtsStorage.setSize(size);
        dtsStorageMapper.insert(dtsStorage);
        return dtsStorageMapper.selectById(dtsStorage.getId());
    }

    @Override
    public Object findAll(Integer page, Integer limit, String key, String name, String sort, String order) {
        Page<DtsStorage> dtsStoragePage = dtsStorageMapper.selectPage(new Page<>(page, limit),
                new QueryWrapper<DtsStorage>()
                        .like(!StringUtils.isEmpty(name), "name", name)
                        .eq(!StringUtils.isEmpty(key), "key", key)
                        .orderBy(!StringUtils.isEmpty(sort), "Asc".equalsIgnoreCase(order), sort));
        Map<String,Object> map=new HashMap<>();
        map.put("total",dtsStoragePage.getTotal());
        map.put("items",dtsStoragePage.getRecords());
        return ResponseUtil.ok(map);
    }

    @Override
    public DtsStorage update(DtsStorage dtsStorage, byte[] file, String ext, int size, String originalFilename) throws Exception {
        String fileName = UUID.randomUUID().toString() +"."+ ext;
        String url = AliyunOSSUtil.uploadImage(aliyunOssEndpoint,
                aliyunOssAccessKeyId,
                aliyunOssAccessKeySecret,
                aliyunOssBucketName,
                fileName,
                file);
        dtsStorage.setName(originalFilename);
        dtsStorage.setKey(fileName);
        dtsStorage.setUrl(url);
        dtsStorage.setUpdateTime(new Date());
        dtsStorage.setSize(size);
        dtsStorage.setType(ext);
        dtsStorageMapper.update(dtsStorage,new QueryWrapper<DtsStorage>().eq("id",dtsStorage.getId()));

        return dtsStorageMapper.selectById(dtsStorage.getId());
    }

    @Override
    public Object delete(DtsStorage dtsStorage) {
        dtsStorageMapper.deleteById(dtsStorage.getId());
        return ResponseUtil.ok();
    }


}
